package morning.cat.study.quartz;

import morning.cat.study.quartz.job.MyJobTask2;
import morning.cat.study.quartz.util.DateUtil;

import org.quartz.JobBuilder;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

/**
 * Hello world!
 *
 */
public class Test2JobDataMap {
	public static void main(String[] args) {

		// 1.JobDetail
		JobDataMap newJobDataMap = new JobDataMap();
		newJobDataMap.put("lover", "gmt");
		JobDetail myJobDetail = JobBuilder.newJob(MyJobTask2.class)
				.withIdentity("myJob", "JobDetailGroup")
				.usingJobData(newJobDataMap)
				.usingJobData("bir", 1993)
				.usingJobData("hit", "talk").build();

		// 2.Trigger
		Trigger myTrigger = TriggerBuilder
				.newTrigger()
				.withIdentity("myTrigger", "TriggerGroup")
				.usingJobData("bir", 1995)
				.withSchedule(
						SimpleScheduleBuilder.simpleSchedule()
								.withIntervalInSeconds(2).repeatForever())
				.usingJobData("msg", "相遇不易，且行且惜").startNow().build();

		// 3.Scheduler
		SchedulerFactory factory = new StdSchedulerFactory();

		try {
			Scheduler scheduler = factory.getScheduler();
			scheduler.scheduleJob(myJobDetail, myTrigger);
			scheduler.start();
			DateUtil.printNowForString("Scheduler");
		} catch (SchedulerException e) {
			e.printStackTrace();
		}

	}
}
